﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using GomMinhLongDO;
using GomMinhLongBL;

namespace GomMinhLong.AdminCP.PageUC
{
    public partial class ucProductCategory : System.Web.UI.UserControl
    {
        cmsLoaiSanPhamDO objLSanPham = new cmsLoaiSanPhamDO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["PID"] != null)
            {
                objLSanPham.LoaiSanPhamID = int.Parse(Request.QueryString["PID"].ToString());
                InitForm();
                if (!IsPostBack)
                {
                    Ultility.ddlDatabinder(ddlDanhMucCha, cmsLoaiSanPhamDO.LOAISANPHAMID_FIELD, cmsLoaiSanPhamDO.TENLOAISANPHAM_FIELD, new DataView(new cmsLoaiSanPhamBL().SelectAll(0), "LoaiSanPhamID <> " + int.Parse(Request.QueryString["PID"].ToString()), "", DataViewRowState.CurrentRows));
                }
            }
            else
            {
                Ultility.ddlDatabinder(ddlDanhMucCha, cmsLoaiSanPhamDO.LOAISANPHAMID_FIELD, cmsLoaiSanPhamDO.TENLOAISANPHAM_FIELD, new cmsLoaiSanPhamBL().SelectAll(0));
            }
        }

        protected void InitForm()
        {
            objLSanPham = new cmsLoaiSanPhamBL().Select(objLSanPham);
            txtTenLSanPham.Text = objLSanPham.TenLoaiSanPham.ToString();
            txtMoTa.Text = objLSanPham.MoTa;
            txteTenLSanPham.Text = objLSanPham.eTenSanPham;
            try
            {
                ddlDanhMucCha.SelectedValue = objLSanPham.ParentID.ToString();
            }
            catch
            {
                
                throw;
            }
            txteMoTa.Text = objLSanPham.eMoTa;
        }

        protected cmsLoaiSanPhamDO SetObject()
        {
            cmsLoaiSanPhamDO obj = new cmsLoaiSanPhamDO();
            obj.TenLoaiSanPham = txtTenLSanPham.Text;
            obj.MoTa = txtMoTa.Text;
            obj.ParentID = 0;
            obj.eMoTa = txteMoTa.Text;
            obj.eTenSanPham = txteTenLSanPham.Text;
            obj.ParentID =int.Parse(ddlDanhMucCha.SelectedValue);
            return obj;
        }
        long i;
        protected void btnLuu_Click(object sender, EventArgs e)
        {
            cmsLoaiSanPhamDO obj = SetObject();
            if (objLSanPham.LoaiSanPhamID <= 0)
            {
                i = new cmsLoaiSanPhamBL().Insert(obj);
                if (i > 0)
                    Functions.Alert("Thêm mới thành công", "Default.aspx?Page=ListProductCategory");
                else
                {
                    Functions.Alert("Lỗi!", Request.Url.AbsolutePath);
                }
            }
            else
            {
                new cmsLoaiSanPhamBL().Update(obj);
                Functions.Alert("Cập nhật thành công", "Default.aspx?Page=ListProductCategory");
            }
        }

        protected void btnHuy_Click(object sender, EventArgs e)
        {

        }
    }
}